package indi.cloud.common.service.impl;

import indi.cloud.common.model.esresult.EsEnterpriseListEntity;
import indi.cloud.common.service.EsSearchService;
import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yangyifan
 * @version 1.0
 * @date 2024/1/29 10:47
 * @Description
 */
@Service
public class EnterpriseListServiceImpl implements EsSearchService {
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

//    @Resource
//    private EnterpriseListRepository enterpriseListRepository;

    @Override
    public SearchHits<EsEnterpriseListEntity> find(String key, Object value) {
        //构造查询条件
        String searchTerm = "\"query\":{\n" +
                "        \"bool\":{\n" +
                "            \"must\":[\n" +
                "                {\n" +
                "                    \"match\":{\n" +
                "                        \"ent_type\":\"T001\"\n" +
                "                    }\n" +
                "                },\n" +
                "                {\n" +
                "                    \"match\":{\n" +
                "                        \"financeIndicator.index_code.keyword\":\"ZB_0110\"\n" +
                "                    }\n" +
                "                },\n" +
                "                {\n" +
                "                    \"match\":{\n" +
                "                        \"financeIndicator.year\":2021\n" +
                "                    }\n" +
                "                },\n" +
                "                {\n" +
                "                    \"match\":{\n" +
                "                        \"financeIndicator.index_value\":53.0\n" +
                "                    }\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    }";
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("key", searchTerm)).build();
        return elasticsearchRestTemplate.search(searchQuery, EsEnterpriseListEntity.class);
    }

    @Override
    public JSONObject paging(Integer currentPage, Integer limit) {
        if (currentPage == null || currentPage < 0 || limit == null || limit <= 0) {
            throw new RuntimeException("请输入合法的分页参数");
        }
        // 分页列表查询
        // 旧版本的 Repository 中的 search 方法被废弃了。
        // 这里采用 ElasticSearchRestTemplate 或 ElasticsearchOperations 来进行分页查询

        JSONObject jsonResult = new JSONObject(true);
        NativeSearchQuery query = new NativeSearchQuery(new BoolQueryBuilder());
        query.setPageable(PageRequest.of(currentPage, limit));

        // 方法1：
        SearchHits<EsEnterpriseListEntity> searchHits = elasticsearchRestTemplate.search(query, EsEnterpriseListEntity.class);



        // 方法2：
        // SearchHits<Article> searchHits = elasticsearchOperations.search(query, Article.class);

        List<EsEnterpriseListEntity> articles = searchHits.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        jsonResult.put("count", searchHits.getTotalHits());
        jsonResult.put("articles", articles);
        System.out.println(jsonResult);
        return jsonResult;
    }
}
